package com.blockchain.coupon.service.impl;

import com.blockchain.coupon.dao.BankMapper;
import com.blockchain.coupon.entity.BankStaff;
import com.blockchain.coupon.service.BankService;
import com.blockchain.coupon.sha3.Sha3;
import com.blockchain.coupon.util.Encryption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by flynn on 17-3-8.
 */
@Service
public class BankServiceImpl implements BankService{

    @Autowired
    public BankMapper bankMapper;
    /**
     * 银行员工注册
     * @param bankStaff
     * @return resultCode
     *      1: 注册成功
     *      2: 已存在用户
     *      3: 注册失败
     *
     */
    @Override
    public String bankStaffRegister(BankStaff bankStaff) {
        String resultCode = "";
        // 1. 查询账户是否存在
        BankStaff bs = this.bankMapper.queryBankStaffByAccount(bankStaff.getBankStaffAccount());
        if(bs != null) {
            resultCode = "2";       // 用户已存在
        }else {
            // 不存在相同用户
            // 生成id
            bankStaff.setBankStaffId(UUID.randomUUID().toString());
            // 生成盐值
            bankStaff.setBankStaffSalt(Encryption.createSalt());
            // 插入合约地址
            bankStaff.setBankContractAddress("bankContractAddress");
            // 加密密码
            bankStaff.setBankStaffPassword(Sha3.sha3(bankStaff.getBankStaffPassword()+bankStaff.getBankStaffSalt()));
            // 插入到数据库中
            if(this.bankMapper.insertBankStaff(bankStaff)==1) {
                resultCode = "1";
            }else {
                resultCode = "3";
            }
        }
        return resultCode;
    }

    /**
     * 银行员工登录
     * @param account
     * @param password
     * @return Map<Strng, Object>
     *     resultCode:
     *          1: 登录成功
     *          2: 不存在此用户
     *          3: 密码错误
     *     bankstaff
     */
    @Override
    public Map<String, Object> bankStaffLogin(String account, String password) {
        Map<String, Object> map = new HashMap<>();
        String resultCode;
        // 查询是否存在此用户
        BankStaff bankStaff = this.bankMapper.queryBankStaffByAccount(account);
        if(bankStaff == null) {
            resultCode = "2";
        }else {
            if(bankStaff.getBankStaffPassword().equals(Sha3.sha3(password + bankStaff.getBankStaffSalt()))) {
                resultCode = "1";
                map.put("bankStaff", bankStaff);
            }else {
                resultCode = "3";
            }
        }
        map.put("resultCode", resultCode);
        return map;
    }

    /**
     * 通过或拒绝商户申请
     * @param status
     * @param id
     * @return
     */
    @Override
    public String merchantPass(String status, String id) {
        return null;
    }
}
